package user

import (
	"context"
	"github.com/asim/go-micro/v3"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"server/proto/user"
	"server/utils"
)

// Register godoc
//	@Summary		用户注册
//	@Description	用户注册
//	@Tags			用户模块
//	@Accept			json
//	@Produce		json
//	@Param			username    formData     string  false  "username"  Format(email)
//  @Param			password    formData     string  false  "password"  Format(pass)
//  @Param			code        formData     string  false  "code"      Format(code)
//	@Success		200		{object}  user.UserResponse
//	@Failure		500		{object}  user.UserResponse
//	@Router			/user/register [post]
func Register(router *gin.Context) {
	username := router.PostForm("email")
	password := router.PostForm("pass")
	code := router.PostForm("code")

	email := utils.VerifyEmail(username) //检验邮箱是否合理
	if email {
		//register service
		service := micro.NewService()

		moduleService := user.NewUserModuleService("develop_user", service.Client())

		user := user.UserRequest{User: username, Password: password, Code: code}

		rep, err := moduleService.UserRegister(context.TODO(), &user)

		if err != nil {
			router.JSON(http.StatusOK, gin.H{
				"code": 500,
				"msg":  "用户注册失败 请重试",
			})
		} else {
			router.JSON(http.StatusOK, gin.H{
				"code": rep.Code,
				"msg":  rep.Message,
			})
		}
	} else {
		router.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "邮箱格式不对",
		})
	}
}



// Login godoc
//	@Summary		用户登陆
//	@Description	用户登陆
//	@Tags			用户模块
//	@Accept			json
//	@Produce		json
//	@Param			username    formData     string  false  "username"  Format(email)
//  @Param			password    formData     string  false  "password"  Format(pass)
//	@Success		200		{object}  model.LoginResponse
//	@Failure		500		{object}  model.LoginResponse
//	@Router			/user/login [post]
func Login(router *gin.Context) {
	username := router.PostForm("username")
	password := router.PostForm("password")

	email := utils.VerifyEmail(username) //检验邮箱是否合理
	if email {
		service := micro.NewService()

		moduleService := user.NewUserModuleService("develop_user", service.Client())

		//user module  micro微服务
		rep, err := moduleService.UserLogin(context.TODO(), &user.UserRequest{User: username, Password: password})
		if err != nil {
			router.JSON(http.StatusOK, gin.H{
				"code": 500,
				"msg":  "账号或密码错误请重试",
			})
		} else {
			//进行token认证
			token, _ := utils.GenToken(username, utils.FrontUserExpireDuration, utils.FrontUserSecretKey)
			router.JSON(http.StatusOK, gin.H{
				"code":     rep.Code,
				"msg":      rep.Message,
				"token":    token,
				"username": username,
			})

			type LoginResponse struct {
				code     int32
				msg      string
				token    string
				username string
			}
			var ss LoginResponse
			log.Println(ss)
		}
	} else {
		router.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "请输入正确邮箱",
		})
	}
}

// Sendemail godoc
//	@Summary		发送邮件
//	@Description	发送邮件
//	@Tags			用户模块
//	@Accept			json
//	@Produce		json
//	@Param			username    formData     string  false  "username"  Format(email)
//	@Success		200		{object}    user.UserResponse
//	@Failure		500		{object}	user.UserResponse
//	@Router			/user/sendEmail [post]
func SendEmail(ctx *gin.Context) {
	form := ctx.PostForm("email")

	flag := utils.VerifyEmail(form) //检验邮箱是否合法
	if flag {

		service := micro.NewService()

		moduleService := user.NewUserModuleService("develop_user", service.Client())

		rep, err := moduleService.SendEmail(context.TODO(), &user.SendEmailRequest{Email: form})
		if err != nil {
			ctx.JSON(http.StatusOK, gin.H{
				"code": 500,
				"msg":  "邮件发送失败",
			})
			return
		}

		ctx.JSON(http.StatusOK, gin.H{
			"code": rep.Code,
			"msg":  rep.Message,
		})
	} else {
		ctx.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "请输入正确邮箱",
		})
	}
}

// FindUser godoc
//	@Summary		用户找回
//	@Description	用户找回
//	@Tags			用户模块
//	@Accept			json
//	@Produce		json
//	@Param			email    formData     string  false  "username"  Format(email)
//	@Param			code     formData     string  false  "code"  Format(code)
//	@Param			pass     formData     string  false  "pass"  Format(pass)
//	@Success		200		{object}    user.UserResponse
//	@Failure		500		{object}	user.UserResponse
//	@Router			/user/findUser [post]
func FindUser(ctx *gin.Context) {
	email := ctx.PostForm("email")
	code := ctx.PostForm("code")
	pass := ctx.PostForm("pass")

	service := micro.NewService()

	moduleService := user.NewUserModuleService("develop_user", service.Client())

	rep, err := moduleService.FindUser(context.TODO(), &user.FindUserRequest{
		Email: email,
		Code:  code,
		Pass:  pass,
	})
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "请求重置密码接口失败",
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code": rep.Code,
		"msg":  rep.Message,
	})

}

// Feedback godoc
//	@Summary		用户反馈
//	@Description	用户反馈
//	@Tags			用户模块
//	@Accept			json
//	@Produce		json
//	@Param			email    formData     string  false  "username"  Format(email)
//	@Param			order_info    formData     string  false  "order_info"  Format(order_info)
//	@Param			feedback    formData     string  false  "feedback"  Format(feedback)
//	@Success		200		{object}    user.UserResponse
//	@Failure		500		{object}	user.UserResponse
//	@Router			/user/feedback [post]
func Feedback(ctx *gin.Context) {
	email := ctx.PostForm("email")
	feedback := ctx.PostForm("feedback")
	order_info := ctx.PostForm("order_info")

	service := micro.NewService()

	moduleService := user.NewUserModuleService("develop_user", service.Client())

	rep, err := moduleService.UserFeedback(context.TODO(), &user.UserFeedbackRequest{
		User:      email,
		Feedback:  feedback,
		OrderInfo: order_info,
	})
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "提交反馈失败",
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code": rep.Code,
		"msg":  rep.Message,
	})
}

// GetUserAccount godoc
//	@Summary		获取用户账号余额
//	@Description	获取用户账号余额
//	@Tags			用户模块
//	@Accept			json
//	@Produce		json
//	@Param			user    formData     string  false  "user"  Format(user)
//	@Success		200		{object}    user.GetUserAccountResponse
//	@Failure		500		{object}	user.UserResponse
//	@Router			/user/GetUserAccount [get]
func GetUserAccount(ctx *gin.Context) {
	//获取用户名称
	query := ctx.Query("user")

	service := micro.NewService()

	moduleService := user.NewUserModuleService("develop_user", service.Client())

	rep, err := moduleService.GetUserAccount(context.TODO(), &user.GetUserAccountRequest{User: query})
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "获取用户余额失败",
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code": rep.Code,
		"msg":  rep.Msg,
		"num":  rep.Num,
	})
}

// UserMint godoc
//	@Summary		用户充值
//	@Description	用户充值
//	@Tags			用户模块
//	@Accept			json
//	@Produce		json
//	@Param			user    formData     string  false  "user"  Format(user)
//	@Param			num    formData     int32  false  "num"  Format(num)
//	@Success		200		{object} user.UserResponse
//	@Failure		500		{object}	user.UserResponse
//	@Router			/user/mint [post]
func UserMint(ctx *gin.Context) {
	num := ctx.PostForm("num")
	users := ctx.PostForm("user")

	service := micro.NewService()

	moduleService := user.NewUserModuleService("develop_user", service.Client())

	rep, err := moduleService.UserMint(context.TODO(), &user.UserMintRequest{
		Num:  utils.StrToInt(num),
		User: users,
	})
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "用户充值失败",
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code": rep.Code,
		"msg":  rep.Message,
	})
}



